package com.kum.controller;

import com.alibaba.fastjson.JSONObject;
import com.kum.domain.constant.AjaxResult;
import com.kum.domain.dto.SignDto;
import com.kum.domain.entity.SysSign;
import com.kum.service.SignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @version V1.0
 * @Package com.kum.controller
 * @auhter 枯木Kum
 * @date 2021/3/9-7:46 PM
 */

@RestController
@RequestMapping("/system/sign")
public class SignController {

    @Autowired
    private SignService signService;


    /**
     * 获取今日签到列表与统计信息
     *
     * @return
     */
    @GetMapping("/list")
    public AjaxResult list() {
        JSONObject jsonObject = new JSONObject();
        List<SysSign> cacheSignList = signService.getCacheSignList();
        //根据时间进行排序
        cacheSignList.sort((a, b) ->
                (int) (b.getSignDate().getTime() - a.getSignDate().getTime())

        );
        jsonObject.put("sign_list", cacheSignList);
        jsonObject.put("statistic", signService.statistic(cacheSignList));
        return AjaxResult.success(jsonObject);
    }

    /**
     * 发起签到
     * @param signDto
     * @return
     */
    @PostMapping("/start")
    public AjaxResult sign(@RequestBody SignDto signDto) {
        return signService.startSign(signDto)
                ? AjaxResult.success("签到成功") : AjaxResult.error("签到失败");

    }

    @GetMapping("/attendance/list/{type}")
    public AjaxResult getAttendanceList(@PathVariable("type") String type) {
        return AjaxResult.success(signService.getAttendanceList(type));
    }

    @PostMapping("/attendance/list")
    public AjaxResult getAttendanceListByDate(@RequestBody JSONObject jsonObject){
        Date start = jsonObject.getDate("start");
        Date end = jsonObject.getDate("end");
        return AjaxResult.success(signService.getAttendanceList(start,end));
    }

}
